package com.example.myapplication.miadapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.mi.MiObject;

import java.util.List;

public class MiAdapter extends RecyclerView.Adapter<MiAdapter.VH> {
    private List<MiObject> items;

    public MiAdapter(List<MiObject> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public MiAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.mi_recycle,parent,false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MiAdapter.VH holder, int position) {
        holder.miName.setText(items.get(position).getDeviceName());
        holder.miTip.setText(items.get(position).getTip());
        holder.miImg.setImageDrawable(ResourcesCompat.getDrawable(holder.miImg.getResources(),items.get(position).getObjectIconID() ,null));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
    public static class VH extends RecyclerView.ViewHolder{
        public ImageView miImg;
        public TextView miName;
        public TextView miTip;
        public VH(@NonNull View itemView) {
            super(itemView);
            miImg=itemView.findViewById(R.id.miImg);
            miName=itemView.findViewById(R.id.miName);
            miTip=itemView.findViewById(R.id.miTip);
        }

    }
}
